package com.li.controller;

import com.li.pojo.User;
import com.li.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class UserController {
    @Resource
    private UserService usi;
    //以下是登录
    @RequestMapping(value = "/user/login")
    public String userLogin(
            @RequestParam("uno") String uno,
            @RequestParam("password") String password,
            HttpSession session,Model model){

        User user=new User();
        user.setUno(uno);
        user.setUpassword(password);
        System.out.println(user.getUno());
        if (usi.findUserByUsernameAndPassowrd(user)!=null){
            session.setAttribute("loginUser",uno);
            //return "redirect:/regist.html";
            return "redirect:/main.html";
        }else{
            model.addAttribute("message","用户名或者密码错误");
            return  "index";
        }
    }
    //以下是注册
    @RequestMapping(value = "/user/regist",method = RequestMethod.POST)
    public boolean userRegister(HttpServletRequest request){
        String uno = request.getParameter("username");
        String upassword = request.getParameter("password");
        User u =new User(uno,upassword);
        return usi.addUser(u);
    }
}
